package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.BuffConfig;
import com.sghd.fight.module.game.config.ElementConfig;
import com.sghd.fight.module.game.config.ElementPk;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.model.Buff;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.room.Robot;
import com.sghd.fight.module.game.service.skill.ConfigService;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author frank
 * @date 2018-12-05 10:17
 * @desc 缠绕
 **/
@Component
public class EnwindEffect implements Effect {
    @Autowired
    private ConfigService configService;

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        int buffId = MapUtils.getInteger(ctx, Effect.BUFF_ID);
        BuffConfig buffConfig = configService.getBuff(buffId);
        int level = owner.getSkillLevel(config.getId());
        ctx.put(LEVEL, level);
        int extrTime = 0;
        int extrValue = 0;
        //额外BUFF时间
        if (owner.getType() != UnitEnum.ROBOT || (owner.getType() == UnitEnum.ROBOT && owner instanceof Robot)) {
            Map<ChessType, Integer> attr2Level = ((Player) owner).getElement2Level();
            int elLevel = attr2Level.get(config.getType());
            for (; elLevel > 0; elLevel--) {
                ElementPk pk = ElementPk.valueOf(config.getType(), elLevel);
                ElementConfig attrConfig = room.getConfigService().getAttr(pk);
                if (logger.isDebugEnabled()) {
                    logger.debug("玩家[{}]五行额外控制时间：{}", owner.getId(), attrConfig.getAttr(room.getType()).getExtraCtrl());
                }
                //五行额外控制
                extrTime += attrConfig.getAttr(room.getType()).getExtraCtrl();
                //额外伤害
                extrValue += attrConfig.getAttr(room.getType()).getInHarm();
            }
        }
        ctx.put(EXTRTIME, extrTime);
        ctx.put(EXTRVALUE, extrValue);
        Buff buff = getBuff(config, buffConfig, ctx, owner, target);
        context.addBuff(target, buff);
        //context.addValue(target, UnitValue.HP, -buff.getValue());//初始扣血
        //targetReport.addValue(ValueItem.valueOf(UnitValue.HP ,-buff.getValue()));

        targetReport.addBuff(BuffReport.valueOf(buffId, buff.getType(), buff.getValue(), buff.getTime(), buff.getLevel()));
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_ENWIND;
    }
}
